#include "sketch.h"

//Sketch::Sketch(Modeling& _mod) : modeling(_mod) {}
Sketch::~Sketch(){}

void Sketch::setTraceFactory(TraceFactory* factory){
    this->traceFactory = factory;
}


void Sketch::addCursor(Cursor &cursor){
    map<int, Trace>::iterator it = this->traces.find( cursor.getId() );
    if ( it == traces.end() ){
        this->traces[cursor.getId()] = traceFactory->createTrace(cursor.getId());
    }
    this->traces[cursor.getId()].addCursor(cursor);
}


void Sketch::addTrace(Trace &t){
    this->traces[t.getId()] = t;
    
//    Scene *scene = modeling.getScene();
//    scene->addSegment( Segment( t.getCursors()[0].getScenePoint(), 
//                                t.getCursors()[1].getScenePoint()) );
}

map<int, Trace>& Sketch::getTraces(){
    return this->traces;
}

void Sketch::clear(){
    this->traces.clear();
}

//TODO Mudar o beautification para trabalhar com <map>
void Sketch::beautifyTrace(Trace &trace){
    if ( traces.empty() ){
        Beautification::position(trace);
        addTrace(trace);
    }
    else if ( trace.size() > 2 ){
        Beautification::position(trace);
        
        vector<Trace> ts;
        for (map<int, Trace>::iterator it=traces.begin(); it != traces.end(); it++){
            ts.push_back(it->second);
        }
        
        if (traces.size() > 0){
            Beautification::connections(ts, trace);
        }

        for (int i = 0; i < ts.size(); i++){
            traces[ts[i].getId()] = ts[i];
        }
        addTrace(trace);
    }
//    this->drawTraces();
}     



int Sketch::processPolygon(int objId){
//    int faceId = 0;

//    vector< vector<Cursor> > polygons;
//    if( this->traces.size() > 1 ){
//        vector<Cursor> poly;

//        vector<Trace> ts;
//        
//        for (map<int, Trace>::iterator it=traces.begin(); it != traces.end(); it++){
//            ts.push_back( this->traces[it->first] );
//        }
//        for(int i = 0; i < this->traces.size(); i++){
//            if (ts.size() > 0 and poly.size() == 0){
//                poly.push_back(ts[0][0]);
//                poly.push_back(ts[0][-1]);
//                ts.erase(ts.begin() );
//            }
//            for (int j = 0; j < ts.size(); j++){
//                
//                if (poly.back().getScenePoint().distance(ts[j][0].getScenePoint()) < 1.0){
//                    poly.push_back(ts[j][-1]);
//                    ts.erase(ts.begin()+j );
//                    break;
//                }
//                if (poly.back().getScenePoint().distance(ts[j][1].getScenePoint()) < 1.0){
//                    poly.push_back(ts[j][0]);
//                    ts.erase(ts.begin()+j );
//                    break;
//                }
//            }
//            if (poly.back().getScenePoint().distance(poly.front().getScenePoint()) < 1.0){
//                Scene *scene = modeling.getScene();
//                polygons.push_back(poly);
//                
//                this->traces.clear();
//                for (int k = 0; k < ts.size(); k++){
//                    this->traces[ts[k].getId()] = ts[k];
//                }
//                
//                // Mudar poly para vector<Point> e retirar o for abaixo
//                vector<Point> points;
//                Point v1, v2;
//                for (int z = 0; z < poly.size()-1; z++){
//                    v1 = poly[z].getScenePoint();
//                    v2 = poly[(z+1)%poly.size()].getScenePoint();
//                    scene->removeSegment( Segment( v1, v2) );
//                    
//                    points.push_back(poly[z].getScenePoint()+Point(0,0.001,0));
//                }                
//                
//                Face f = modeling.factory.createFace(points, objId);
//                modeling.addFace(f, objId);
//                faceId = f.getId();
//                poly.clear();
//            }
//        }
//    }
//    modeling.updateScene();
//    return faceId;
}        


void Sketch::drawTraces(){
//    modeling.updateScene();  
}
